package com.kapcb.common.function;

import com.kapcb.common.exception.ConditionException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * <a>Title: Assert </a>
 * <a>Author: Kapcb <a>
 * <a>Description: Assert <a>
 *
 * @author Kapcb
 * @version 1.0
 * @date 2024/4/21 13:39
 * @since 1.0
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Assert {

    /**
     * expression 为 true 的时候抛出异常信息
     *
     * @param expression 表达式
     * @return ExceptionHandler
     */
    public static ExceptionHandler isTrue(boolean expression) {
        return errorMessage -> {
            if (expression) {
                throw new ConditionException(errorMessage);
            }
        };
    }

    /**
     * @param expression
     * @return
     */
    public static BranchHandler condition(boolean expression) {
        return (trueHandler, falseHandler) -> {
            if (expression) {
                trueHandler.run();
            } else {
                falseHandler.run();
            }
        };
    }

    public static PresentHandler<?> isBlankOrNoBlank(String str) {

        return (consumer, runnable) -> {
            if (StringUtils.isBlank(str)) {
                runnable.run();
            } else {
                consumer.accept(str);
            }
        };
    }
}
